package com.zhentao.tokenfile;

import com.alibaba.fastjson.JSON;
import com.zhentao.pojo.User;
import com.zhentao.util.JwtService;
import com.zhentao.util.Result;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Component
public class TokenFile implements Filter {
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest request1 = (HttpServletRequest) request;
//        HttpServletResponse response1 = (HttpServletResponse) response;
//        String requestURI = request1.getRequestURI();
//        if("/user/login".equals(requestURI) || "/user/logout".equals(requestURI) ||"/error".equals(requestURI)){
//            chain.doFilter(request, response);
//            return;
//        }
//        String authorization = request1.getHeader("Authorization");
//        if(authorization!=null && authorization.length()>0){
//            if(JwtService.verifyToken(authorization)==1){
//                Map<String, Object> claimsMap = JwtService.getClaimsMap(authorization);
//                Object username = claimsMap.get("username");
//                Object uid = claimsMap.get("uid");
//                Object rName = claimsMap.get("rName");
//                Object perms = claimsMap.get("perms");
//                User user = new User();
//                user.setPerms((List<String>) perms);
//                user.setRName((List<String>) rName);
//                user.setUsername((String) username);
//                user.setUid((Integer) uid);
//                UsernamePasswordAuthenticationToken authenticated = UsernamePasswordAuthenticationToken.authenticated(user, null, user.getAuthorities());
//                authenticated.setDetails(new WebAuthenticationDetailsSource().buildDetails(request1));
//                SecurityContextHolder.getContext().setAuthentication(authenticated);
//                chain.doFilter(request, response);
//                return;
//            }
//        }
//        Result ok = Result.ERROR("token值不够");
//        response1.setContentType("application/json;charset=utf-8");
//        PrintWriter writer = response1.getWriter();
//        String string = JSON.toJSONString(ok);
//        writer.write(string);
//    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        String requestURI = request1.getRequestURI();
        if("/user/login".equals(requestURI) || "/user/logout".equals(requestURI) || "/error".equals(requestURI)){
            chain.doFilter(request, response);
            return;
        }
        String authorization = request1.getHeader("Authorization");
        if(authorization!=null && authorization.length()>0){
            if(JwtService.verifyToken(authorization)==1){
                Map<String, Object> claimsMap = JwtService.getClaimsMap(authorization);
                Object username = claimsMap.get("username");
                Object uid = claimsMap.get("uid");
                Object perms = claimsMap.get("perms");
                Object rName = claimsMap.get("rName");
                User user = new User();
                user.setPerms((List<String>) perms);
                user.setRName((List<String>) rName);
                user.setUid((Integer) uid);
                user.setUsername((String) username);
                UsernamePasswordAuthenticationToken authenticated = UsernamePasswordAuthenticationToken.authenticated(user, null, user.getAuthorities());
                authenticated.setDetails(new WebAuthenticationDetailsSource().buildDetails(request1));
                SecurityContextHolder.getContext().setAuthentication(authenticated);
                chain.doFilter(request, response);
                return;
            }
        }
        Result ok = Result.ERROR("token值不够");
        response1.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response1.getWriter();
        String string = JSON.toJSONString(ok);
        writer.write(string);
    }
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest request1 = (HttpServletRequest) request;
//        HttpServletResponse response1 = (HttpServletResponse) response;
//        String requestURL = request1.getRequestURI();
//        if("/user/login".equals(requestURL) || "/user/logout".equals(requestURL) || "/error".equals(requestURL)){
//            chain.doFilter(request, response);
//            return;
//        }
//        String authorization = request1.getHeader("Authorization");
//        if(authorization!=null&&authorization.length()>0){
//            if(JwtService.verifyToken(authorization)==1){
//                Map<String, Object> claimsMap = JwtService.getClaimsMap(authorization);
//                Object username = claimsMap.get("username");
//                Object uid = claimsMap.get("uid");
//                Object perms = claimsMap.get("perms");
//                Object rName = claimsMap.get("rName");
//                User user = new User();
//                user.setRName((List<String>) rName);
//                user.setPerms((List<String>) perms);
//                user.setUid((Integer) uid);
//                user.setUsername((String) username);
//                UsernamePasswordAuthenticationToken authenticated = UsernamePasswordAuthenticationToken.authenticated(user, null, user.getAuthorities());
//                authenticated.setDetails(new WebAuthenticationDetailsSource().buildDetails(request1));
//                SecurityContextHolder.getContext().setAuthentication(authenticated);
//                chain.doFilter(request, response);
//                return;
//            }
//        }
//        Result ok = Result.ERROR("token值不够");
//        response1.setContentType("application/json;charset=utf-8");
//        PrintWriter writer = response1.getWriter();
//        String string = JSON.toJSONString(ok);
//        writer.write(string);
//    }
}
